package apis

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"net/http"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service/dto"
	cDto "tuowei.com/common/dto"
	"tuowei.com/pkg/xlog"
)

type SysTeachingPoint struct {
	api.Api
	base
}

func (p *SysTeachingPoint) Delete(ctx *gin.Context) {
	req := dto.SysTeachingPointDeleteRequest{}

	err := p.MakeContext(ctx).MakeOrm().Errors
	if err != nil {
		p.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	err = ctx.ShouldBind(&req)
	if err != nil {
		p.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	//校验教学点是否在使用
	for _, id := range req.Ids {
		cou := models.SysColleges{}
		p.Orm.Table(cou.TableName()).Where("deleted_at is null and teaching_point_id=?", id).First(&cou)
		if cou.Id != 0 {
			xlog.Error("教学点正在使用 teaching_point_id:", id)
			p.Error(500, err, "教学点正在使用 不能删除")
			return
		}
		le := models.SysLecturer{}
		p.Orm.Table(le.TableName()).Where("deleted_at is null and teaching_point_id=?", id).First(&le)
		if cou.Id != 0 {
			xlog.Error("教学点正在使用 teaching_point_id:", id)
			p.Error(500, err, "教学点正在使用 不能删除")
			return
		}
		cl := models.SysClass{}
		p.Orm.Table(le.TableName()).Where("deleted_at is null and dept_id=?", id).First(&le)
		if cl.Id != 0 {
			xlog.Error("教学点正在使用 teaching_point_id:", id)
			p.Error(500, err, "教学点正在使用 不能删除")
			return
		}
	}
	affected := p.Orm.Debug().Where("id in (?)", req.Ids).Delete(&models.SysTeachingPoint{}).RowsAffected
	if affected == 0 {
		p.Error(http.StatusInternalServerError, errors.New("影响行数为0"), "影响行数为0")
		return
	}
	p.OK(nil, "success")
}

func (p *SysTeachingPoint) List(ctx *gin.Context) {
	req := dto.SysTeachingPointListRequest{}
	err := ctx.ShouldBind(&req)
	err = p.MakeContext(ctx).MakeOrm().Errors
	if err != nil {
		p.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	list := make([]models.SysTeachingPoint, 0)
	db := p.Orm.Preload("Province").Preload("City").
		Preload("County").Preload("Department").
		Preload("Controllers").Preload("Enrollments")
	var count int64
	if req.Name != "" {
		db = db.Where("name like ?", "%"+req.Name+"%")
	}

	err = db.Scopes(
		cDto.Paginate(req.GetPageSize(), req.GetPageIndex())).Find(&list).Limit(-1).Offset(-1).Count(&count).Error
	if err != nil {
		p.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	p.PageOK(list, int(count), req.GetPageIndex(), req.GetPageIndex(), "success")

}

func (point *SysTeachingPoint) UpdatePoint(ctx *gin.Context) {
	req := dto.SysTeachingPointUpdateRequest{}
	err := point.MakeContext(ctx).MakeOrm().Errors
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	err = ctx.ShouldBind(&req)
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	data := &models.SysTeachingPoint{Id: req.Id}
	err = point.Orm.First(data).Error
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	(&req).Generate(data)
	point.Orm.Where("point_id = ?", data.Id).Delete(models.SysTeachingPoint2Enrollment{})
	point.Orm.Where("point_id = ?", data.Id).Delete(models.SysTeachingPoint2teacher{})
	err = point.Orm.Save(data).Error
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
	} else {
		point.OK(data, "success")
	}
}

// 增加教学点
func (point *SysTeachingPoint) AddPoint(ctx *gin.Context) {
	req := dto.SysTeachingAddPointRequest{}
	err := point.MakeContext(ctx).MakeOrm().Bind(&req, binding.JSON).Errors
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	data := &models.SysTeachingPoint{}
	(&req).Generate(data)
	err = point.Orm.Create(data).Error
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
	} else {
		point.OK(data, "success")
	}
}

func (point *SysTeachingPoint) Detail(ctx *gin.Context) {
	req := &dto.SysTeachingPointDetailRequest{}
	err := point.MakeContext(ctx).MakeOrm().Errors
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	err = ctx.BindUri(&req)
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	detail := models.SysTeachingPoint{Id: req.Id}
	err = point.Orm.Preload("Province").Preload("City").Preload("County").
		Preload("Department").Preload("Controllers").Preload("Enrollments").
		First(&detail).Error
	if err != nil {
		point.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	point.OK(detail, "success")
}
